package com.zstk.frame.config.interceptor;

import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.zstk.frame.util.ZStringUtil;

import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 参数对象为Map的转换器
 * </p>
 *
 * @author ZColin
 * @since 2020/12/11 11:06
 */
public class MapLikeSqlConverter extends AbstractLikeSqlConverter<Map> {
    /**
     * SQL语句百分号
     */
    private final static String SQL_PERCENT = "%";

    @Override
    public void transferWrapper(String field, Map parameter) {
        AbstractWrapper wrapper = (AbstractWrapper) parameter.get(MybatisSqlConstant.MYBATIS_PLUS_WRAPPER_KEY);
        parameter = wrapper.getParamNameValuePairs();
        String[] keys = field.split(MybatisSqlConstant.MYBATIS_PLUS_WRAPPER_SEPARATOR_REGEX);
        // ew.paramNameValuePairs.param1，截取字符串之后，获取第三个，即为参数名
        String paramName = keys[2];
        String mapKey = String.format("%s.%s", MybatisSqlConstant.REPLACED_LIKE_KEYWORD_MARK, paramName);
        if (parameter.containsKey(mapKey) && Objects.equals(parameter.get(mapKey), true)) {
            return;
        }
        if (MybatisSqlConstant.cascade(field)) {
            this.resolveCascadeObj(field, parameter);
        } else {
            Object param = parameter.get(paramName);
            if (this.hasEscapeChar(param)) {
                String paramStr = param.toString();
                handlerParameter(keys[2], paramStr, parameter);
            }
        }
        parameter.put(mapKey, true);
    }

    private void handlerParameter(String field, String paramStr, Map parameter) {
        if (paramStr.startsWith(SQL_PERCENT) && !paramStr.endsWith(SQL_PERCENT)) {
            //左模糊
            parameter.put(field, "^.*" + ZStringUtil.escapeChar(paramStr.substring(1)) + "$");
        } else if (!paramStr.startsWith(SQL_PERCENT) && paramStr.endsWith(SQL_PERCENT)) {
            //右模糊
            parameter.put(field, "^" + ZStringUtil.escapeChar(paramStr.substring(0, paramStr.length() - 1)) + ".*$");
        } else {
            //全模糊
            parameter.put(field, ZStringUtil.escapeChar(paramStr.substring(1, paramStr.length() - 1)));
        }
    }

    @Override
    public void transferSelf(String field, Map parameter) {
        if (MybatisSqlConstant.cascade(field)) {
            this.resolveCascadeObj(field, parameter);
            return;
        }
        if (parameter.containsKey(field)) {
            Object param = parameter.get(field);
            if (this.hasEscapeChar(param)) {
                String paramStr = param.toString();
                handlerParameter(field, paramStr, parameter);
            }
        }
    }

    @Override
    public void transferSplice(String field, Map parameter) {
        if (MybatisSqlConstant.cascade(field)) {
            this.resolveCascadeObj(field, parameter);
            return;
        }
        Object param = parameter.get(field);
        if (this.hasEscapeChar(param)) {
            parameter.put(field, ZStringUtil.escapeChar(param.toString()));
        }
    }

    /**
     * 处理级联属性
     *
     * @param field     级联字段名
     * @param parameter 参数Map对象
     */
    private void resolveCascadeObj(String field, Map parameter) {
        int index = field.indexOf(MybatisSqlConstant.MYBATIS_PLUS_WRAPPER_SEPARATOR);
        Object param = parameter.get(field.substring(0, index));
        if (param == null) {
            return;
        }
        this.resolveObj(field.substring(index + 1), param);
    }

}
